package dearchenpush.good.com.qianfanlive.modules.discover.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.squareup.picasso.Transformation;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import dearchenpush.good.com.qianfanlive.R;
import dearchenpush.good.com.qianfanlive.modules.discover.bean.RecyclerLikeBean;

/**
 * User: sgd(952519440@qq.com)
 * Date: 2016/09/28
 * Time: 15:54
 * progject: QianfanLive
 * Describe：
 */

public class RecyclerLikeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<RecyclerLikeBean.MessageBean.AnchorsBean> mData = new ArrayList<>();
    private Context mContext;

    public RecyclerLikeAdapter(Context context) {
        mContext = context;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_dislikefragment, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ViewHolder holder1 = (ViewHolder) holder;
        holder1.name.setText(mData.get(position).getName());
        holder1.count.setText(mData.get(position).getFocus() + "人观看");
        Picasso.with(mContext).load(mData.get(position).getPic51()).transform(new Transformation() {
            @Override
            public Bitmap transform(Bitmap source) {
                int widthLight = source.getWidth();
                int heightLight = source.getHeight();

                Bitmap output = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);

                Canvas canvas = new Canvas(output);
                Paint paintColor = new Paint();
                paintColor.setFlags(Paint.ANTI_ALIAS_FLAG);

                RectF rectF = new RectF(new Rect(0, 0, widthLight, heightLight));

                canvas.drawRoundRect(rectF, widthLight / 15, heightLight / 15, paintColor);

                Paint paintImage = new Paint();
                paintImage.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_ATOP));
                canvas.drawBitmap(source, 0, 0, paintImage);
                source.recycle();
                return output;
            }

            @Override
            public String key() {
                return "roundcorner";
            }
        }).into(holder1.image);
        if (mData.get(position).getLive() == 1) {
            holder1.live.setVisibility(View.VISIBLE);
        } else {
            holder1.live.setVisibility(View.GONE);
        }
    }

    @Override
    public int getItemCount() {
        return mData != null ? mData.size() : 0;
    }

    public void addAll(List<RecyclerLikeBean.MessageBean.AnchorsBean> dd) {
        mData.addAll(dd);
        notifyDataSetChanged();
    }

    public void clearAll() {
        mData.clear();
        notifyDataSetChanged();
    }


//    static class ViewHolder1 extends RecyclerView.ViewHolder {
//
//        public ViewHolder1(View itemView) {
//            super(itemView);
//        }
//    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.image_disLikeFragment_item)
        ImageView image;
        @BindView(R.id.tv_disLikeFragment_name)
        TextView name;
        @BindView(R.id.tv_disLikeFragment_count)
        TextView count;
        @BindView(R.id.image_disLikeFragment_live)
        ImageView live;

        public ViewHolder(View itemView) {

            super(itemView);
            ButterKnife.bind(this, itemView);

        }
    }
}
